package de.lulusoft.anothertorrent.gui.wizards;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.lulusoft.anothertorrent.core.AnotherTorrentCore;
import de.lulusoft.anothertorrent.core.servers.ServerType;

public class SelectServerTypeWizardPage extends WizardPage {
	
	private Text name = null;
	private Combo type = null;
	private Button useI8 = null;

	protected SelectServerTypeWizardPage(String pageName) {
		super(pageName);
		setTitle("Select Server Type");
		setDescription("Select the server type.");
	}
	
	public String getServerName()
	{
		return name.getText().trim();
	}
	
	public boolean isI8Supported() {
		return useI8.getSelection();
	}


	@Override
	public IWizardPage getNextPage() {
		return ((NewServerWizard)getWizard()).usedServerPage;
	}


	@Override
	public void createControl(Composite parent) {
		Composite main = new Composite(parent, SWT.NONE);
		main.setLayout(new GridLayout(2, false));
		GridData gd = new GridData();
		gd.horizontalAlignment = SWT.FILL;
		gd.grabExcessHorizontalSpace = true;
		GridData gdInput = new GridData();
		gdInput.horizontalAlignment = SWT.FILL;
		gdInput.grabExcessHorizontalSpace = true;
		
		Label lname = new Label(main, SWT.NONE);
		lname.setLayoutData(gd);
		lname.setText(Messages.NewServerWizardPage_4);
		name = new Text(main, SWT.BORDER | SWT.SINGLE);
		name.setLayoutData(gdInput);
		name.addModifyListener(new ModifyListener() {
		
			@Override
			public void modifyText(ModifyEvent e) {
				setPageComplete(isPageComplete());
			}
		});
		
		gdInput = new GridData();
		gdInput.horizontalAlignment = SWT.FILL;
		gdInput.grabExcessHorizontalSpace = true;
		gdInput.horizontalSpan = 2;
		useI8 = new Button(main, SWT.CHECK);
		useI8.setText("XMLRPC I8 Support");
		useI8.setLayoutData(gdInput);
	
		gdInput = new GridData();
		gdInput.horizontalAlignment = SWT.FILL;
		gdInput.grabExcessHorizontalSpace = true;
		Label ltype = new Label(main, SWT.LEFT);
		ltype.setLayoutData(gd);
		ltype.setText("Select Server Type");
		type = new Combo(main, SWT.BORDER);
		type.setLayoutData(gdInput);
		type.add(ServerType.XMLRPC.name());
		type.add(ServerType.SOCKET.name());
		type.select(0);
		type.addModifyListener(new ModifyListener() {
		
			@Override
			public void modifyText(ModifyEvent e) {
				setPageComplete(isPageComplete());
			}
		});
		
		// Fill existing server info
		if (((NewServerWizard)getWizard()).edit)
		{
			name.setText(((NewServerWizard)getWizard()).server.getName());
			useI8.setSelection(((NewServerWizard)getWizard()).server.isI8Support());
			type.setText(((NewServerWizard)getWizard()).server.getType().name());
			
		}
		
		setControl(main);
	}

	@Override
	public boolean isPageComplete() {
		if ( name != null && name.getText().trim().length() > 0 
				&& type != null && type.getSelectionIndex() != -1
				&& (!AnotherTorrentCore.getDefault()
				.getServerManager().serverExists(name.getText().trim()) 
					|| ((NewServerWizard)getWizard()).edit)) // Make sure no existing server has this name if not in edit mode
		{
			if (type.getText().equals(ServerType.XMLRPC.name()))
			{
				((NewServerWizard)getWizard()).usedServerPage = 
					((NewServerWizard)getWizard()).newXmlRcpServerWizardPage;
				return true;
			}
			else if (type.getText().equals(ServerType.SOCKET.name()))
			{
				((NewServerWizard)getWizard()).usedServerPage = 
					((NewServerWizard)getWizard()).newSocketServerWizardPage;
				return true;
			}
		}
		return false;
	}
	

}
